package com.qf.data.service.impl;

import com.qf.common.bean.R;
import com.qf.data.service.CheckCodeService;
import com.qf.data.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author 千锋健哥
 */
@Service
public class CheckCodeServiceImpl implements CheckCodeService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public R save(String phone, String code) {
        try {
            redisUtil.setKey(phone, code, 5, TimeUnit.MINUTES);
            return R.ok("保存验证码成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("保存验证码失败!");
    }

    @Override
    public R<String> getCode(String phone) {
        try {
            String value = redisUtil.getKey(phone, String.class);
            return R.ok(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("验证码错误!");
    }

}
